package com.kadoufall.recommender.web;

import com.kadoufall.recommender.dao.NewsRepository;
import com.kadoufall.recommender.dao.UserCFRecResultRepository;
import com.kadoufall.recommender.dao.UtilRepositoryCustom;
import com.kadoufall.recommender.model.News;
import com.kadoufall.recommender.model.UserCFRecResult;
import com.kadoufall.recommender.service.contentBased.ContentBasedRecommenderService;
import com.kadoufall.recommender.service.hot.HotRecommenderService;
import com.kadoufall.recommender.service.userCF.UserCFRecommenderService;
import com.kadoufall.recommender.service.util.UtilService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/recommend")
public class RecommendController {
    @Autowired
    private UserCFRecommenderService userCFRecommenderService;

    @Autowired
    private ContentBasedRecommenderService contentBasedRecommenderService;

    @Autowired
    private HotRecommenderService hotRecommenderService;

    @Autowired
    private NewsRepository newsRepository;

    @Autowired
    private UtilService utilService;

    @Autowired
    UserCFRecResultRepository userCFRecResultRepository;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String indexTest() {
        return "Recommender for Todayim";
    }

    @RequestMapping(value = "/do_cf/{userId}", method = RequestMethod.GET)
    public List<News> getRecommendNews(@PathVariable long userId) {
        System.out.println("in CF recommend");
        List<News> recNewsList =  userCFRecommenderService.recommendNews(userId);
        List<UserCFRecResult> userCFRecResultList=new ArrayList<>();
        for (News news : recNewsList) {
            UserCFRecResult userCFRecResult =  new UserCFRecResult();
            userCFRecResult.setNewsId(news.getId());
            userCFRecResult.setUserId(userId);
            userCFRecResultList.add(userCFRecResult);
        }
        // 先把数据库中这个用户的结果删除
        userCFRecResultRepository.deleteAll(userCFRecResultRepository.findByUserId(userId));
        // 再保存新数据
        userCFRecResultRepository.saveAll(userCFRecResultList);

        return recNewsList;
    }


    @RequestMapping(value = "/do_cf_for_all/", method = RequestMethod.GET)
    public void recommendNewsForAll() {
        System.out.println("in cf for all recommend");
        userCFRecommenderService.recommendNewsForAll();
    }
    @RequestMapping(value = "/do_cf_limit/{id}", method = RequestMethod.GET)
    public List<News> getLimitRecommendNews(@PathVariable long id) {
        System.out.println("in CF limit recommend");
        return userCFRecommenderService.recommendNewsLimit(id);
    }

    @RequestMapping(value = "/get_cf_result/{id}", method = RequestMethod.GET)
    public List<News> getRecommendNewsResult(@PathVariable long id) {
        System.out.println("get CF result");
        return userCFRecommenderService.getRecommendNewsResult(id);
    }


    @RequestMapping(value = "/vsm/{id}", method = RequestMethod.GET)
    public List<News> getVSMRecommendNews(@PathVariable long id) {
        System.out.println("in VSM recommend");
        return contentBasedRecommenderService.recommendNews(id);
    }

    @RequestMapping(value = "/hot/{id}", method = RequestMethod.GET)
    public List<News> getHottestRecommendNews(@PathVariable long id) {
        System.out.println("in hot recommend");
        return hotRecommenderService.recommendNews(id);
    }

    @RequestMapping(value = "/seen/{id}", method = RequestMethod.GET)
    public List<News> getSeenNews(@PathVariable long id) {
        System.out.println("get seen news " + id);
        return utilService.findSeenNews(id);
    }
}
